package com.itheima.filter;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

//@WebFilter(urlPatterns = "/*", filterName = "DemoFilter")  //拦截路径代表了拦截所有路径
@Slf4j
public class DemoFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        log.info("DemoFilter initialized");
        // 初始化逻辑
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //每次拦截到的请求，就会触发方法，会调用多次
        //发行前
        log.info("DemoFilter doFilter before");

        //发行
        filterChain.doFilter(servletRequest, servletResponse);

        //发行后
        log.info("DemoFilter doFilter after");
    }
    @Override
    public void destroy(){
        // 再次关闭，销毁一次
        log.info("DemoFilter destroyed");
    }
}
